﻿using System;
using System.Security.Cryptography;
using System.Text;

public static class HashGenerator
{
    public static string GenerateHash(string input)
    {
        using (SHA256 sha256Hash = SHA256.Create())
        {
            // 将输入字符串转换为字节数组
            byte[] bytes = Encoding.UTF8.GetBytes(input);

            // 计算哈希值
            byte[] hashBytes = sha256Hash.ComputeHash(bytes);

            // 将字节数组转换为字符串
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < hashBytes.Length; i++)
            {
                builder.Append(hashBytes[i].ToString("x2"));
            }

            return builder.ToString();
        }
    }
    
    public static string CalculateMd5(string filePath)
    {
        using (var md5 = MD5.Create())
        {
            using (var stream = File.OpenRead(filePath))
            {
                var hash = md5.ComputeHash(stream);
                return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
            }
        }
    }
}